package com.itheima.consumer.mq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.lang.model.type.ExecutableType;
import java.util.Arrays;
import java.util.Map;

@Component
@Slf4j
public class SpringRabbitListener {
    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue(Message message) {
        log.info("监听的消息: " + message);
        System.out.println(Arrays.toString(message.getBody()));
        System.out.println(message.getMessageProperties().getMessageId());
//        throw new RuntimeException("我是故意sss的异常");
    }
    @RabbitListener(queues = "work.queue")
    public void listenWorkQueue1(String message) throws InterruptedException {
       System.out.println("用户1监听的消息: " + message);
       Thread.sleep(25);
    }
    @RabbitListener(queues = "work.queue")
    public void listenWorkQueue2(String message) throws InterruptedException {
        System.out.println("用户2监听的消息: " + message);
        Thread.sleep(200);
    }
    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String message) throws InterruptedException {
        System.out.println("消费者1监听fanout.queue1监听的消息: " + message);
        Thread.sleep(200);
    }
    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2(String message) throws InterruptedException {
        System.out.println("消费者2监听fanout.queue2监听的消息: " + message);
        Thread.sleep(200);
    }
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name="direct.queue1",durable="true"),
                    exchange = @Exchange(name = "itcast.direct",type = ExchangeTypes.DIRECT),
                    key = {"red","blue"}
            )
    )
    public void listenDirectQueue1(String message) throws InterruptedException {
        System.out.println("消费者1监听direct.queue2监听的消息: " + message);
        Thread.sleep(200);
    }
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name="direct.queue2",durable="true"),
                    exchange = @Exchange(name = "itcast.direct",type = ExchangeTypes.DIRECT),
                    key = {"red","blue"}
            )
    )
    @RabbitListener(queues = "direct.queue2")
    public void listenDirectQueue2(String message) throws InterruptedException {
        System.out.println("消费者2监听direct.queue2监听的消息: " + message);
        Thread.sleep(200);
    }
    @RabbitListener(queues = "topic.queue1")
    public void listenTopicQueue1(String message) throws InterruptedException {
        System.out.println("消费者1监听topic.queue1监听的消息: " + message);
        Thread.sleep(200);
    }
    @RabbitListener(queues = "topic.queue2")
    public void listenTopicQueue2(String message) throws InterruptedException {
        System.out.println("消费者2监听topic.queue2监听的消息: " + message);
        Thread.sleep(200);
    }
    @RabbitListener(queues = "object.queue")
    public void listenObjectQueue1(Map<Object, Object> map) {
        map.forEach((k,v) -> System.out.println("消费者1监听object.queue监听的消息: " + k + " " + v));
    }
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name="dlx.queue",durable="true"),
                    exchange = @Exchange(name = "dlx.direct",type = ExchangeTypes.DIRECT),
                    key = {"hi"}
            )
    )
    public void listenDlxQueue(String message) throws InterruptedException {
        System.out.println("消费者监听dlx.queue监听的消息: " + message);
    }
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name="delay.queue",durable="true"),
                    exchange = @Exchange(name = "delay.direct",delayed = "true"),
                    key = {"hi"}
            )
    )
    public void listenDelayQueue(String message) throws InterruptedException {
        System.out.println("消费者监听dlx.queue监听的消息: " + message);
    }
}